package com.shenyi.common.service;


import com.shenyi.common.api.ISysMenuService;
import com.shenyi.common.api.ISysRoleMenuService;
import com.shenyi.common.api.ISysUserService;
import com.shenyi.common.dao.SysMenuMapper;
import com.shenyi.common.entity.SysMenuDO;
import com.shenyi.core.utils.Constant;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;


@Service("sysMenuService")
public class SysMenuServiceImpl implements ISysMenuService {
    @Autowired
    private SysMenuMapper sysMenuMapper;
    @Autowired
    private ISysUserService sysUserService;
    @Autowired
    private ISysRoleMenuService sysRoleMenuService;

    @Override
    public List<SysMenuDO> queryListParentId(Long parentId, List<Long> menuIdList) {
		List<SysMenuDO> menuList = sysMenuMapper.queryListParentId(parentId);
		if(menuIdList == null){
			return menuList;
		}

		List<SysMenuDO> userMenuList = new ArrayList<>();
		for(SysMenuDO menu : menuList){
			if(menuIdList.contains(menu.getMenuId())){
				userMenuList.add(menu);
			}
		}
		return userMenuList;
    }

    @Override
    public List<SysMenuDO> queryNotButtonList() {
		return sysMenuMapper.queryNotButtonList();
    }

    @Override
    public List<SysMenuDO> getUserMenuList(Long userId) {
        //系统管理员，拥有最高权限
        if (userId == 1) {
            return getAllMenuList(null);
        }

        //用户菜单列表
        List<Long> menuIdList = sysUserService.queryAllMenuId(userId);
        return getAllMenuList(menuIdList);
    }

    @Override
    public SysMenuDO queryObject(Long menuId) {
		return sysMenuMapper.queryObject(menuId);
    }

    @Override
    public List<SysMenuDO> queryList(Map<String, Object> map) {
		return sysMenuMapper.queryList(map);
    }

    @Override
    public int queryTotal(Map<String, Object> map) {
        return sysMenuMapper.queryTotal(map);
    }

    @Override
    public void save(SysMenuDO menu) {
		sysMenuMapper.save(menu);
    }

    @Override
    public void update(SysMenuDO menu) {
		sysMenuMapper.update(menu);
    }

    @Override
    @Transactional
    public void deleteBatch(Long[] menuIds) {
        sysMenuMapper.deleteBatch(menuIds);
    }

    @Override
    public List<SysMenuDO> queryUserList(Long userId) {
		return sysMenuMapper.queryUserList(userId);
    }

    /**
     * 获取所有菜单列表
     */
    private List<SysMenuDO> getAllMenuList(List<Long> menuIdList) {
        //查询根菜单列表
        List<SysMenuDO> menuList = queryListParentId(0L, menuIdList);
        //递归获取子菜单
        getMenuTreeList(menuList, menuIdList);

        return menuList;
    }

    /**
     * 递归
     */
    private List<SysMenuDO> getMenuTreeList(List<SysMenuDO> menuList, List<Long> menuIdList) {
        List<SysMenuDO> subMenuList = new ArrayList<SysMenuDO>();

        for (SysMenuDO entity : menuList) {
			if(entity.getType() == Constant.MenuType.CATALOG.getValue()){//目录
				entity.setList(getMenuTreeList(queryListParentId(entity.getMenuId(), menuIdList), menuIdList));
			}
			subMenuList.add(entity);
        }

        return subMenuList;
    }
}
